/**
 * Escriba un programa para calcular la paga semanal de un empleado. El usuario introduce el 
 * número de horas de tiempo normal trabajado en la semana. Luego selecciona su precio por hora 
 * a partir de un cuadro de la lista. También puede ingresar el número de horas extras trabajadas. 
 * Las horas extras se pagan al doble de la tarifa por hora.
 */
package com.virglinio.calculapagasemanal;
/*
 *  Principal.java is part of calcula-paga-semanal
 *
 *  calcula-paga-semanal is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  calcula-paga-semanal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with calcula-paga-semanal.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author Mario Fernando Virglinio
 * @see <a href="http://www.virglinio.com.ar">http://www.virglinio.com.ar</a>
 *
 */
public class Principal {

	private JFrame frmCalculaPagaSemanal;
	private JTextField txtHorasTrabajadas;
	private JTextField txtHorasExtras;
	private JTextField txtTotalAPagar;
	private JCheckBox chckbxHorasExtras;
	private JList listPrecioPorHora;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Principal window = new Principal();
					window.frmCalculaPagaSemanal.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Principal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCalculaPagaSemanal = new JFrame();
		frmCalculaPagaSemanal.setTitle("Calcula Paga Semanal");
		frmCalculaPagaSemanal.setBounds(100, 100, 450, 300);
		frmCalculaPagaSemanal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmCalculaPagaSemanal.getContentPane().setLayout(null);
		
		JLabel lblHorasTrabajadas = new JLabel("Horas Trabajadas");
		lblHorasTrabajadas.setBounds(12, 45, 146, 15);
		frmCalculaPagaSemanal.getContentPane().add(lblHorasTrabajadas);
		
		txtHorasTrabajadas = new JTextField();
		txtHorasTrabajadas.setBounds(160, 43, 114, 19);
		frmCalculaPagaSemanal.getContentPane().add(txtHorasTrabajadas);
		txtHorasTrabajadas.setColumns(10);
		
		chckbxHorasExtras = new JCheckBox("Horas Extras");
		chckbxHorasExtras.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (chckbxHorasExtras.isSelected()) {
					txtHorasExtras.setEnabled(true);
				} else {
					txtHorasExtras.setEnabled(false);
				}
			}
		});
		chckbxHorasExtras.setBounds(8, 87, 129, 23);
		frmCalculaPagaSemanal.getContentPane().add(chckbxHorasExtras);
		
		txtHorasExtras = new JTextField();
		txtHorasExtras.setEnabled(false);
		txtHorasExtras.setBounds(160, 89, 114, 19);
		frmCalculaPagaSemanal.getContentPane().add(txtHorasExtras);
		txtHorasExtras.setColumns(10);
		
		listPrecioPorHora = new JList();
		listPrecioPorHora.setModel(new AbstractListModel() {
			String[] values = new String[] {"18.00", "18.50", "19.00", "19.50", "20.00", "20.50", "21.00", "21.50", "22.00"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		listPrecioPorHora.setBounds(316, 44, 114, 170);
		frmCalculaPagaSemanal.getContentPane().add(listPrecioPorHora);
		
		JLabel lblPrecioPorHora = new JLabel("Precio por hora");
		lblPrecioPorHora.setBounds(311, 17, 119, 15);
		frmCalculaPagaSemanal.getContentPane().add(lblPrecioPorHora);
		
		JButton btnCalcularPaga = new JButton("Calcular Paga");
		btnCalcularPaga.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double horasTrabajadas, totalPagar;
				horasTrabajadas = Double.parseDouble(txtHorasTrabajadas.getText());
				
				if (chckbxHorasExtras.isSelected()) {
					String strLineaList = (String)listPrecioPorHora.getSelectedValue();
					double horasExtras = Double.parseDouble(txtHorasExtras.getText());
					totalPagar = horasTrabajadas * Double.parseDouble(strLineaList) + 2 * horasExtras * Double.parseDouble(strLineaList);
					txtTotalAPagar.setText(Double.toString(totalPagar));
				} else {
					String strLineaList = (String)listPrecioPorHora.getSelectedValue();
					totalPagar = horasTrabajadas * Double.parseDouble(strLineaList);
					txtTotalAPagar.setText(Double.toString(totalPagar));
				}
				
			}
		});
		btnCalcularPaga.setBounds(12, 167, 146, 25);
		frmCalculaPagaSemanal.getContentPane().add(btnCalcularPaga);
		
		JLabel lblTotalAPagar = new JLabel("Total a pagar");
		lblTotalAPagar.setBounds(12, 227, 125, 15);
		frmCalculaPagaSemanal.getContentPane().add(lblTotalAPagar);
		
		txtTotalAPagar = new JTextField();
		txtTotalAPagar.setBounds(160, 225, 114, 19);
		frmCalculaPagaSemanal.getContentPane().add(txtTotalAPagar);
		txtTotalAPagar.setColumns(10);
	}
}
